package com.lichuang.dedao.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.dedao.user.entity.Collection;
import com.lichuang.dedao.user.entity.User;
import com.lichuang.dedao.user.enums.CollectStatus;
import com.lichuang.dedao.user.mapper.CollectionMapper;
import com.lichuang.dedao.user.service.ICollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 收藏 服务实现类
 * </p>
 *
 * @author hanghang
 * @since 2019-07-14
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements ICollectionService {

    @Autowired
    private CollectionServiceImpl collectionService;

    @Override
    @Cacheable(cacheNames = "collection", key = "#userId", unless = "#result == null")
    public List<Collection> selectCollectionsWithRedis(Integer userId) {
        Collection collectionParam = new Collection();
        collectionParam.setUserId(userId)
                .setStatus(CollectStatus.COLLECTED.getValue());
        QueryWrapper queryWrapper = new QueryWrapper(collectionParam);
        List<Collection> collections = baseMapper.selectList(queryWrapper);
        return collections;
    }

    @Override
    public List<Collection> selectCollectionsByType(Integer userId, Integer collectionType) {
        List<Collection> collections = collectionService.selectCollectionsWithRedis(userId);
        if (collections != null) {
            List<Collection> collectionsWithType = new LinkedList<>();
            for (Collection collection : collections) {
                if (collection.getCollectionType() == collectionType) {
                    collectionsWithType.add(collection);
                }
            }
            return collectionsWithType;
        }
        return null;
    }

    @Override
    @CachePut(cacheNames = "collection", key = "#userId")
    public List<Collection> updateCollectionsWithRedis(Integer userId, Integer collectionType, Integer objectId) {
        Collection collectionParam = new Collection();
        collectionParam.setUserId(userId)
                .setObjectId(objectId);
        QueryWrapper queryWrapper = new QueryWrapper(collectionParam);
        Integer count = baseMapper.selectCount(queryWrapper);
        if (count == 0) {
            collectionParam.setCollectionType(collectionType)
                    .setStatus(CollectStatus.COLLECTED.getValue())
                    .setCollectionTime(LocalDateTime.now());
            baseMapper.insert(collectionParam);
        } else {
            Collection collection = baseMapper.selectOne(queryWrapper);
            Integer status = collection.getStatus();
            CollectStatus collectStatus = CollectStatus.CollectStatusOf(status);
            switch (collectStatus) {
                case COLLECTED:
                    collectStatus = CollectStatus.NOT_COLLECT;
                    break;
                case NOT_COLLECT:
                    collectStatus = CollectStatus.COLLECTED;
                    break;
            }
            collection.setStatus(collectStatus.getValue());
            baseMapper.updateById(collection);
        }
        Collection collection = new Collection();
        collection.setUserId(userId)
                .setStatus(CollectStatus.COLLECTED.getValue());
        List<Collection> collections = baseMapper.selectList(new QueryWrapper<>(collection));
        return collections;
    }
}
